/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"FB6C8ADA-D896-4331-A7A9-D0A5CF54A81A"}
 */
function finalizaCompra(event) {
	
	databaseManager.setAutoSave(false)
	
	if (hacerFactura() == 0)
	{
		plugins.dialogs.showErrorDialog('Error','Ocurrio un error en la factura','OK')
		return
	}
	if (!databaseManager.saveData())
	{
		plugins.dialogs.showErrorDialog('Error','Ocurrio un error en la grabación','OK')
	}
	else
	{
		globals.borrarCarrito()
		gusuario_to_carrito_cab.newRecord()
		databaseManager.setAutoSave(true)
		plugins.dialogs.showInfoDialog('Exito','La compra se finalizo correctamente','OK')
	}
}

/**
 * 
 * @private
 * 
 * @properties={typeid:24,uuid:"AE4DB6E6-ED1C-4E92-9121-F5BD6357B46A"}
 */
function hacerFactura()
{
	/**@type {JSFoundSet<db:/infocom/fac_cabecera>} */
	var $facturas = databaseManager.getFoundSet('infocom','fac_cabecera')
	$facturas.newRecord()
	
	$facturas.nro_factura		= gusuario_to_carrito_cab.carrito_cab_id
	$facturas.cliente_id 		= globals.gUsuarioId
	$facturas.userv_id 			= globals.gUservId
	$facturas.fecha 			= application.getServerTimeStamp()
	$facturas.subtotal1 		= gusuario_to_carrito_cab.subtotal_1
	$facturas.subtotal2 		= gusuario_to_carrito_cab.subtotal_2
	$facturas.descuento 		= gusuario_to_carrito_cab.descuento_imp
	$facturas.iva1_imp  		= gusuario_to_carrito_cab.iva_imp
	$facturas.iva1_porc 		= gusuario_to_carrito_cab.iva_porc
	$facturas.total     		= gusuario_to_carrito_cab.total
	$facturas.marca_anulacion 	= 0
	$facturas.importe_en_letras = globals.nro_a_letra($facturas.total)
	
	var $fac_dtl = $facturas.fac_cabecera_to_fac_detalle
	for (var i = 1 ; i <= gusuario_to_carrito_cab.carrito_cab_to_carrito_dtl.getSize() ; i++)
	{
		var $dtl = gusuario_to_carrito_cab.carrito_cab_to_carrito_dtl.getRecord(i)
		
		$fac_dtl.newRecord()
		$fac_dtl.producto_id 		= $dtl.producto_id
		$fac_dtl.precio_unitario 	= $dtl.precio_unitario
		$fac_dtl.cantidad 			= $dtl.cantidad
		$fac_dtl.total_renglon 		= $dtl.total
		$fac_dtl.userv_id 			= globals.gUservId
		$fac_dtl.descuento_importe 	= $dtl.descuento_imp
		$fac_dtl.descuento_porc 	= $dtl.descuento_porc
	}
	return 1
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"9AFC5288-FFA2-41C5-9908-03725E91E83E"}
 */
function borrar(event) {
	globals.gDialog('¿Seguro?','¿Desea eliminar este producto del carrito?','Si','borrarProducto','No')
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"E88AA6E7-66C3-4BF3-95C7-16E7BC6E6882"}
 */
function onShow(firstShow, event) {
	if (firstShow)
		controller.loadRecords(gusuario_to_carrito_cab.carrito_cab_to_carrito_dtl)
}

/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @private
 *
 * @properties={typeid:24,uuid:"698B7967-1C72-42AB-96FD-70F138BB09C9"}
 */
function onDataChangeCantidad(oldValue, newValue, event) {
	globals.calcularTotalesCarrito()
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"20052C84-E95C-4BBD-80DE-6BCAB020197A"}
 */
function iraPpal(event) {
	globals.show('productos_filtros')
}
